const log = require('../util/log'),
  crypto = require('crypto')

var queryObj = {
  app_id: '2016060201470909',
  biz_content: JSON.stringify({
    out_biz_no: require('../mng/alipayMng').out_biz_no,
    payee_type: 'ALIPAY_LOGONID',
    payee_account: 'yehr@vip.qq.com',
    amount: '1',
    payer_show_name: '旅悠',
    payee_real_name: '',
    remark: '【99.9元5件】淘鲜团 虾滑 挪威三文鱼手打虾滑150g 85%虾肉颗粒看得见  火锅丸子 火锅 食材 海鲜水产;【99.9元5件】淘>鲜团 大颗粒 手打 虾滑 150g 虾肉占比'
  }),
  charset: 'UTF8',
  format: 'json',
  method: 'alipay.fund.trans.toaccount.transfer',
  sign_type: 'RSA2',
  timestamp: require('../../lib/formatDate')(new Date()),
  version: '1.0'
}

var queryString = ''
for (var key in queryObj)
  queryString += key + '=' + queryObj[key] + '&'
queryString = queryString.substring(0, queryString.length - 1)
// log(queryString)

var sign = crypto.createSign('RSA-SHA256')
sign.update(queryString)
var result = sign.sign(`-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDRHshAcczm+JGr5aUasjj5Mbz9n4KbwlynKIK3sC8TO17YIkgIc74Xydyk0QIUyFf7SYqMslnA1e4BOXqTysdXlf+0Sc7iatDH1+suc4Ixmu4eMveigNB2VojC2+S5sMeCvM6P60vWwNOruQwe27z5hpcVzWCZnaMZUDZOb46PtD6fANrZ8nqCjqVofYcNRtSZ9IqCib7Q4lOpLpm91OxI6MHXsJWe3g+m3dhf0SQOJokF+f8SsAA54EeB1pcNvTkPVTarIYGTE8QJ3bbi2Xdj+1rN2YVy8cRVM7mc1s8Oa/Q/RRtwuWVQlyRxVuZcd/oGYDnnShmqJlLrHQ2QkaUvAgMBAAECggEAFkaGJD87CVtvAPWMWcSkRv8iV43XNspEXlX1XqW4zf6kArfcHnJdzGDnTPMqmpYgsRbLWGNjJiRjnuPFh9qw00aaH1VBYWLpbHA2kBMjyi7rsHHTaQ75pbFgDgmoWpNX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khC9YwT3N2p4Hc=
-----END PRIVATE KEY-----`, 'base64')
// log(result)

queryObj.sign = result
var biz_content = queryObj.biz_content
delete queryObj.biz_content
var timestamp = queryObj.timestamp.replace(/ /g, '+').replace(/:/g, '%3A')
delete queryObj.timestamp
var url = 'https://openapi.alipay.com/gateway.do?' + require('querystring').stringify(queryObj) + '&timestamp=' + timestamp
// log(url)

require('../util/getHtml')({
  url: url,
  data: 'biz_content=' + biz_content
}).then(result => {
  log(result)
}, result => {
  log(result)
})
